package cloud.ben.smallscalepigfarmmanagementsystem.controller.user;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.User;
import cloud.ben.smallscalepigfarmmanagementsystem.service.IUserService;
import cloud.ben.smallscalepigfarmmanagementsystem.utils.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.PastOrPresent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2022/3/2 23:57
 */
@RestController
@Api(value = "用户管理", tags = "用户管理")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册接口",notes = "",httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(type = "query", name = "id",
//                    value = "主键id", required = false, dataTypeClass = Integer.class),
//            @ApiImplicitParam(type = "query", name = "userName",
//                    value = "用户名", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "password",
//                    value = "用户密码", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "email",
//                    value = "用户邮箱", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "phoneNumber",
//                    value = "用户手机号", required = true, dataTypeClass = String.class),
//    })
    public RespBean registerUser(@Valid User user, BindingResult bindingResult) {
        logger.info(user.toString());
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("注册失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        user.setRoles("ROLE_User");
        return iUserService.insertUser(user);
    }

    @PostMapping("/admin/user")
    @ApiOperation(value = "增加用户接口",notes = "", httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(type = "query", name = "id",
//                    value = "主键id", required = false, dataTypeClass = Integer.class),
//            @ApiImplicitParam(type = "query", name = "userName",
//                    value = "用户名", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "password",
//                    value = "用户密码", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "email",
//                    value = "用户邮箱", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "phoneNumber",
//                    value = "用户手机号", required = true, dataTypeClass = String.class),
//    })
    public RespBean insertUser(@Valid User user,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("增加失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        if (user.getRoles()==null){
            user.setRoles("ROLE_User");
        }
        return iUserService.insertUser(user);
    }

    @DeleteMapping("/admin/user/{id}")
    @ApiOperation(value = "根据id删除用户信息接口",notes = "", httpMethod = "DELETE")
//    @ApiImplicitParam(type = "query", name = "id",
//            value = "id", required = true, dataTypeClass = String.class)
    public RespBean deleteUser(@PathVariable("id") String id){
        if (id == null) {
            logger.error("您输入的数据为空，请重新输入");
            return RespBean.error("您输入的数据为空，请重新输入");
        }
        Integer returnId = null;
        try {
            returnId = Integer.parseInt(id);
        } catch (Exception exception) {
            logger.error("您传入的id不合法，请重新输入");
            return RespBean.error("您传入的id不合法，请重新输入");
        }
        return iUserService.deleteUser(returnId);
    }


    @PutMapping("/admin/user")
    @ApiOperation(value = "修改用户接口",notes = "", httpMethod = "PUT")
//    @ApiImplicitParams({
//            @ApiImplicitParam(type = "query", name = "id",
//                    value = "主键id", required = false, dataTypeClass = Integer.class),
//            @ApiImplicitParam(type = "query", name = "userName",
//                    value = "用户名", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "password",
//                    value = "用户密码", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "email",
//                    value = "用户邮箱", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(type = "query", name = "phoneNumber",
//                    value = "用户手机号", required = true, dataTypeClass = String.class),
//    })
    public RespBean updateUser(@Valid User user,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("修改失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        return iUserService.updateUser(user);
    }

    @GetMapping("/admin/user")
    @ApiOperation(value = "分页查询用户信息接口",notes = "", httpMethod = "GET")
//    @ApiImplicitParams({
//            @ApiImplicitParam(type = "query", name = "pageNumber",
//                    value = "第几页", required = true, dataTypeClass = Integer.class),
//            @ApiImplicitParam(type = "query", name = "pageSize",
//                    value = "每页几条信息", required = true, dataTypeClass = Integer.class)
//    })
    public RespBean selectUserListByPage(@RequestParam Integer pageNumber, @RequestParam Integer pageSize){
        return iUserService.selectUserByPage(pageNumber, pageSize);
    }
}